package com.gin.kotlin.gf2gamecommunity.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gin.kotlin.form.ComplaintUserForm
import com.gin.kotlin.form.PrivacyForm
import com.gin.kotlin.form.UserIdForm
import com.gin.kotlin.form.UserInfoForm
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedLiveData
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedPagingData
import com.gin.kotlin.gf2gamecommunity.base.livedata.ParamCachedLiveData
import com.gin.kotlin.gf2gamecommunity.callback.AbstractNothingCallback
import com.gin.kotlin.gf2gamecommunity.pagingsource.CommentUserPagingSource
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.comment.UserComment
import com.gin.kotlin.response.other.AvatarInfo
import com.gin.kotlin.response.user.PrivacyConfig
import com.gin.kotlin.response.user.SetPrivacyBody
import com.gin.kotlin.response.user.UserInfo
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class UserCenterViewModel : ViewModel() {
    val userInfo = ParamCachedLiveData<Res<UserInfo.Body>, UserInfo, Int>(
        requestMethod = { userApi().getMemberInfo(UserIdForm(it)) },
        bodyTransform = { it.data?.user },
    )
    val avatarList = CachedLiveData<Res<AvatarInfo.Body>, List<AvatarInfo>>(
        requestMethod = { App.INSTANCE.api.otherApi.getAvatarList() },
        bodyTransform = { it.data?.avatarList },
    )

    val commentUser = CachedPagingData<UserComment, Int>(scope = viewModelScope) { CommentUserPagingSource(it) }

    val privacy = ParamCachedLiveData<Res<PrivacyConfig.Body>, List<PrivacyConfig>, Int>(
        requestMethod = { userApi().getMemberPrivacy() },
        bodyTransform = { it.data?.privacy },
    )

    /**
     * 修改隐私设置，成功后重新请求
     * @param userId Int
     * @param showType Int
     */
    fun setPrivacy(userId: Int, showType: Int) = userApi().setPrivacy(PrivacyForm(showType)).enqueue(object : Callback<Res<SetPrivacyBody>?> {
        override fun onResponse(call: Call<Res<SetPrivacyBody>?>, response: Response<Res<SetPrivacyBody>?>) {
            privacy.fetch(userId, true)
        }

        override fun onFailure(call: Call<Res<SetPrivacyBody>?>, throwable: Throwable) {
            App.onFailed(throwable)
        }
    })

    /**
     * 关注/取关用户
     * @param userId Int
     * @param onSuccess (message: String) -> Unit
     */
    fun follow(userId: Int, onSuccess: (message: String) -> Unit) {
        userApi().followUser(userId).enqueue(object : AbstractNothingCallback() {
            override fun onSuccess(message: String) {
                App.toast(message)
                onSuccess(message)
            }
        })
    }

    /**
     * 修改资料
     * @param form UserInfoForm
     * @param onSuccess (message: String) -> Unit
     */
    fun modify(form: UserInfoForm, onSuccess: (message: String) -> Unit) = userApi().modify(form).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess(message)
        }
    })

    /**
     * 举报用户
     * @param form ComplaintUserForm
     * @param onSuccess (message: String) -> Unit
     */
    fun complaint(form: ComplaintUserForm, onSuccess: (message: String) -> Unit = {}) = userApi().complaint(form).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess(message)
        }
    })


    private fun userApi() = App.INSTANCE.api.userApi

    companion object {
        private val TAG = UserCenterViewModel::class.java.simpleName
    }
}